package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.OrderBean;
import com.project.dao.IOrderDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class OrderDaoImpl extends BaseDao implements IOrderDao {
    /**
     * 分页查询
     * @param pageNo 页码
     * @return
     */
    @Override
    public CutPageBean<OrderBean> cutAll(int pageNo) {
        CutPageBean<OrderBean> cut = new CutPageBean();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_order limit ?,?");
            ps.setObject(1,(pageNo-1)*CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);
            res= ps.executeQuery();
            while (res.next()){
                OrderBean order  = this.getUser();
                cut.getList().add(order);
            }
            ps = con.prepareStatement("select count(*) c from t_order");
            res=ps.executeQuery();
            while (res.next()){
                cut.setCount(res.getInt("c"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return cut;
    }

    /**
     * 添加
     * @param order 对象
     */
    @Override
    public void add(OrderBean order) {
        this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_order(o_name,o_birthday,o_address)"+"values(?,?,?)");
            ps.setObject(1,order.getUserName());
            ps.setObject(2,order.getOrderDate());
            ps.setObject(3,order.getAddress());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    /**
     * 按id删除
     * @param id 订单id
     */
    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps=con.prepareStatement("delete from t_order where pk_orderId=?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    /**
     * 修改订单地址
     * @param id  订单id
     * @param address 订单地址
     */
    @Override
    public void update(int id, String address) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_order set o_address=? where pk_orderId=?");
            ps.setObject(1,address);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    @Override
    public OrderBean findById(int id) {
        OrderBean order= new OrderBean();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_order where pk_orderId=?");
            ps.setObject(1,id);
            res = ps.executeQuery();
            while (res.next()){
                order=this.getUser();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return order;

    }
    /**
     * 添加
     * @return
     */
    public OrderBean getUser(){
        OrderBean order = new OrderBean();
        try {
            order.setUserName(res.getString("o_name"));
            order.setId(res.getInt("pk_orderId"));
            order.setAddress(res.getString("o_address"));
            String bir = res.getString("o_birthday");
            order.setOrderDate(bir!=null? LocalDate.parse(bir):null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    public static void main(String[] args) {
        IOrderDao dao = new OrderDaoImpl();
//        System.out.println(dao.cutAll(1));
//        System.out.println( dao.findById(2));
//        dao.del(9);
//        dao.add(new OrderBean("赵云","青羊区"));
//        dao.update(1,"武侯区");
    }
}
